<template>
  <div class="app-container">
    <div class="ele-table-tool-default flex">
      <el-button type="primary" icon="el-icon-plus" @click="showForm">新增记录</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        prop="date"
        label="序号"
        width="60"
        align="center"
      />
      <el-table-column
        prop="account"
        label="账户"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="money"
        label="充值金额"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="picture"
        label="充值凭证"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="created_at"
        label="充值时间"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="status"
        label="审核状态"
        align="center"
      >
        <template #default="scope">
          <el-tag v-if="+scope.row.status === 1" type="success">通过</el-tag>
          <el-tag v-if="+scope.row.status === 0" type="primary">待审核</el-tag>
          <el-tag v-if="+scope.row.status === -1" type="danger">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        label="操作"
        align="center"
        width="240"
      >
        <template #default="scope">
          <el-link type="primary" icon="el-icon-edit" class="mr12" @click="edit(scope.row)">修改</el-link>
          <el-link type="success" icon="el-icon-key" class="mr12" @click="verify(scope.row)">审核</el-link>
          <el-link type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <page-container
      v-show="false"
      :list-obj="listObj"
      @changeCurrentPage="changeCurrentPage"
      @changePagesize="changePagesize"
    />
    <main-form ref="mainForm" @submitForm="submitForm" />
    <verify ref="verify" @verifyOne="verifyOne" />
  </div>
</template>

<script>
import MainForm from './components/main-form'
import Verify from './components/verify'
import PageContainer from '@/components/PageContainer'
import { getAll, addOne, updateOne, deleteOne, verifyOne } from '@/api/recharge'
import { removeEmptyField } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  components: {
    MainForm,
    Verify,
    PageContainer
  },
  data() {
    return {
      tableData: [],
      row: null,
      params: { status: '' },
      listObj: null
    }
  },
  computed: {
    ...mapGetters(['project'])
  },
  created() {
    this.getAll()
  },
  methods: {
    showForm() {
      this.$refs.mainForm.show()
    },
    async getAll() {
      this.params.project_id = this.project.id
      const { data } = await getAll(removeEmptyField(this.params))
      this.listObj = data
      this.tableData = data.data
    },
    submitForm(formData) {
      if (formData.id) {
        this.updateOne(formData)
      } else {
        this.addOne(formData)
      }
    },
    async addOne(formData) {
      formData.project_id = this.project.id
      await addOne(formData).then((res) => {
        this.$message.success(res.msg)
        this.getAll()
        this.$refs.mainForm.hide()
      }).catch(() => {})
    },
    async updateOne(formData) {
      await updateOne(formData.id, formData).then((res) => {
        this.$message.success(res.msg)
        this.getAll()
        this.$refs.mainForm.hide()
      }).catch(() => {})
    },
    async deleteOne(id) {
      await deleteOne(id).then((res) => {
        this.$message.success(res.msg)
        this.getAll()
      }).catch(() => {})
    },
    async verifyOne(formData) {
      await verifyOne(formData.recharge_id, formData).then((res) => {
        this.$message.success(res.msg)
        this.getAll()
        this.$refs.verify.hide()
      }).catch(() => {})
    },
    edit(row) {
      this.$refs.mainForm.show(row)
    },
    verify(row) {
      this.$refs.verify.show(row)
    },
    del(row) {
      this.$confirm('确定要删除此条记录吗？', '操作确认', { type: 'warning' }).then(() => {
        this.deleteOne(row.id)
      }).catch(() => {})
    },
    changeCurrentPage(val) {
      this.$emit('changeCurrentPage', val)
    },
    changePagesize(val) {
      this.$emit('changePagesize', val)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
